#include "NetworkMenuController.h"

NetworkMenuController::NetworkMenuController(Model* m, View* v, NetworkPat* net): Controller(m, v)
{
  _model = (NetworkMenuModel*)m;
  _view = (NetworkMenuView*)v;
  _net = net;
  newGameState = GameState::NetworkMenu;
  ShouldReset = false;
}

NetworkMenuController::~NetworkMenuController(void)
{
}

void NetworkMenuController::HandleMessages(std::list<Message*> *l)
{
  if(l == NULL)
    return;
  newGameState = GameState::NetworkMenu;
  if(ShouldReset)
  {
    _net->Reset();
  }
  while(!l->empty())
  {
    Message* temp = l->front();
    l->pop_front();
	  if(temp->GetMessageType() == MessageType::USER_INPUT)
	  {
	    switch(temp->GetMessageContent())
	    {
	      case GameAction::MOVE_UP:
		      _model->IncrementMenuItemUp();
          break;
		    case GameAction::MOVE_DOWN:
		      _model->IncrementMenuItemDown();
		      break;
	      //BIG_TODO : FIX THESE OPTIONS ONCE ALL GAME STATES ARE IN
		    case GameAction::SELECT:
          int selection = _model->GetSelectedMenuItem();
          if(selection == 2)
            newGameState = GameState::MainMenu;
          if(selection == 1)
          {
            if(_net->IsValid())
            {
              newGameState = GameState::ClientSeek;
            }
            else
            {
              newGameState = GameState::MainMenu;
              ShouldReset = true;
            }
          }
          if(selection == 0)
          {
            if(_net->IsValid())
            {
              newGameState = GameState::ServerListen;
            }
            else
            {
              ShouldReset = true;
              newGameState = GameState::MainMenu;
            }
          }
          break;
	    }
	    delete temp;
	  }
  }
}

GameState NetworkMenuController::Update(void)
{
  _view->DrawScreen();
  return newGameState;
}

void NetworkMenuController::PassMessage(Message* msg)
{
}

void NetworkMenuController::ProcessMessages(void)
{
}
